package top.leyi.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.ITalkService;
import top.leyi.entity.Talk.SimpleTalk;
import top.leyi.entity.Talk.Talk;
import top.leyi.entity.user.User;
import top.leyi.exception.TalkNotFoundException;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@RequestMapping("/api/talk")

@RestController
public class TalkHandle {
    @Autowired
    ITalkService talkService;


    /**
     * @desc 获取指定范围的说说
     * @param offset
     * @return
     */
    @RequestMapping(value = "/{offset}", method = RequestMethod.GET)
    public Result doGet(@PathVariable(value = "offset") int offset,
                        @RequestParam(value = "id", required = false, defaultValue = "" + Integer.MAX_VALUE) int userId,
                        @RequestParam(value = "key", required = false, defaultValue = "") String key) {

         List<SimpleTalk> simpleTalks = talkService.getTalks(offset, userId, key);
         return new Result(200,simpleTalks );
    }
    /**
     * @desc 根据id查说说
     *
     */

    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public Result doGetId(
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = false, defaultValue = "" + Integer.MAX_VALUE) int talkId,
                        @RequestParam(value = "token", required = false, defaultValue = "") String token,
                        @RequestParam(value = "userId", required = false, defaultValue = "" + Integer.MAX_VALUE) int userId) {


        User user = new User();
        user.setId(userId);
        user.setToken(token);
        return new Result(200,talkService.getTalkWithId(talkId, user) );
    }
    /**
     * @desc 获取个人发布过的历史全部说说
     * @param offset
     * @return
     */
    @RequestMapping(value = "/history/{offset}", method = RequestMethod.GET)
    public Result doGetOwner(@PathVariable(value = "offset") int offset,
                        @RequestParam(value = "id", required = false, defaultValue = "" + Integer.MAX_VALUE) int userId,
                        @RequestParam(value = "key", required = false, defaultValue = "") String key) {

        List<SimpleTalk> simpleTalks = talkService.getHistoryTalks(offset, userId, key);
        return new Result(200,simpleTalks );
    }

    /**
     * @desc 获取最热门的10条的说说
     * @return
     */
    @RequestMapping(value = "/hot", method = RequestMethod.GET)
    public Result doGetHot( @RequestParam(value = "id", required = false, defaultValue = "" + Integer.MAX_VALUE) int userId) {
        return new Result(200, talkService.getHotTalks( userId));
    }
    /**
     *  @desc 上传说说
     * @param content
     * @param pics
     * @param userId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST )
    public Result doPut(@RequestParam(value = "content", required = false) String content,
                                              @RequestParam(value = "pics", required = false) String pics,
                                              @RequestParam(value = "id", required = true) int userId,
                                              @RequestParam(value = "token", required = true) String token){

        Talk talk = new Talk();
        talk.setContent(content);
        talk.setUser_id( userId );
        talk.setPics(pics);

        User user = new User();
        user.setId(userId);
        user.setToken(token);

        return new Result(200, talkService.addTalk(talk, user));
    }

    /**
     * @desc 对说说进行修改，如评论，点赞等
     * @param talkId
     * @param content
     * @param pics
     * @param userId
     * @return
     * @throws TalkNotFoundException
     */
    @RequestMapping(value = "/comment", method = RequestMethod.POST )
    public Result doPost(@RequestParam("talkId") int talkId ,
                                              @RequestParam("content") String content,
                                              @RequestParam(value = "pics", required = false) String pics,
                                              @RequestParam("id")int userId,
                                              @RequestParam(value = "token", required = true) String token) throws TalkNotFoundException {

        Talk comment = new Talk();
        comment.setPics(pics);
        comment.setId(talkId);
        comment.setContent(content);
        comment.setUser_id(userId);

        User user = new User();
        user.setId(userId);
        user.setToken(token);

        Map<String, ? super Object> map = new HashMap<>();

        return new Result(200,  talkService.commentTalk(comment,user));
    }

    /**
     * 点赞说说
     * @param talkId
     * @param userId
     * @param token
     * @return
     * @throws TalkNotFoundException
     */
    @RequestMapping(value = "/good", method = RequestMethod.POST)
    public Result goodOpreation(@RequestParam("talkId") int talkId ,
                                              @RequestParam("id")int userId,
                                              @RequestParam(value = "token", required = true) String token) throws TalkNotFoundException {

        Talk good = new Talk();
        good.setUser_id(userId);
        good.setId(talkId);

        User user = new User();
        user.setId(userId);
        user.setToken(token);

        talkService.goodTalk(good, user);
        return ResultGenerator.OK.getResult();
    }

    /**
     * @desc 删除说说
     * @param talkId
     * @param userId
     * @param token
     * @return
     * @throws TalkNotFoundException
     */
    @GetMapping("/delete")
    public Result deleteTalk(@RequestParam("id") int talkId ,
                             @RequestParam("userId")int userId,
                             @RequestParam(value = "token", required = true) String token) throws TalkNotFoundException {
        Talk talk = new Talk();
        talk.setId(talkId);
        talk.setUser_id(userId);

        talkService.deleteTalk(talk);
        return ResultGenerator.OK.getResult();
    }

    /**
     * @desc 删除评论说说
     * @param commentId
     * @param userId
     * @param token
     * @return
     * @throws TalkNotFoundException
     */
    @GetMapping("/comment/delete")
    public Result deleteComment(@RequestParam("id") int commentId ,
                             @RequestParam("userId")int userId,
                             @RequestParam(value = "token", required = true) String token) throws TalkNotFoundException {

        Talk talk = new Talk();
        talk.setId(commentId);
        talk.setUser_id(userId);


        talkService.deleteComment(talk);
        return ResultGenerator.OK.getResult();
    }
}
